package com.yanqu.road.dao.impl.activity.peakstronghold;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.peakstronghold.CrossUnionAbilityCacheData;
import com.yanqu.road.entity.activity.peakstronghold.PeakStrongholdUploadMarkData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PeakStrongholdUploadMarkDaoImpl extends TempDataObjectDao<PeakStrongholdUploadMarkData> {
    public static String getPeakStrongholdUploadMarkDataSql = "select * from t_u_peak_stronghold_upload_mark where activityId=?";

    public Map<Integer, PeakStrongholdUploadMarkData> getPeakStrongholdUploadMarkData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, PeakStrongholdUploadMarkData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getPeakStrongholdUploadMarkDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                PeakStrongholdUploadMarkData data = parse(rs);
                dataMap.put(data.getUnionGroup(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updatePeakStrongholdUploadMarkDataSql = "update t_u_peak_stronghold_upload_mark set round=?,waitUploadAbilityUsers=?,waitUploadDefendUsers=?,unionIds=?,outUnionIds=? where activityId=? and unionGroup=?";

    @Override
    protected boolean updateData(PeakStrongholdUploadMarkData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getWaitUploadAbilityUsers())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getWaitUploadDefendUsers())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUnionIds())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getOutUnionIds())));

        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUnionGroup()));
        if (getDb().executeNoneQuery(updatePeakStrongholdUploadMarkDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertPeakStrongholdUploadMarkDataSql = "insert into t_u_peak_stronghold_upload_mark(activityId,unionGroup,round,waitUploadAbilityUsers,waitUploadDefendUsers,unionIds,outUnionIds) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(PeakStrongholdUploadMarkData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUnionGroup()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getWaitUploadAbilityUsers())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getWaitUploadDefendUsers())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUnionIds())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getOutUnionIds())));
        if (getDb().executeNoneQuery(insertPeakStrongholdUploadMarkDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected PeakStrongholdUploadMarkData parseData(ResultSet rs) throws SQLException {
        PeakStrongholdUploadMarkData data = new PeakStrongholdUploadMarkData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUnionGroup(rs.getInt("unionGroup"));
        data.setRound(rs.getInt("round"));
        data.setWaitUploadAbilityUsers(JSON.parseObject(rs.getString("waitUploadAbilityUsers"), new TypeReference<LinkedList<Long>>() {
        }));
        data.setWaitUploadDefendUsers(JSON.parseObject(rs.getString("waitUploadDefendUsers"), new TypeReference<LinkedList<Long>>() {
        }));
        data.setUnionIds(JSON.parseObject(rs.getString("unionIds"), new TypeReference<List<String>>() {
        }));

        String outUnionIds = rs.getString("outUnionIds");
        if (!StringUtils.isNullOrEmpty(outUnionIds)){
            data.setOutUnionIds(JSON.parseObject(outUnionIds, new TypeReference<List<String>>() {
            }));
        }

        return data;
    }
}
